package com.eudi.web.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eudi.dubbo.common.User;
import com.eudi.user.service.IUserService;

@Controller
@RequestMapping(value = "/user")
public class UserController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mv = new ModelAndView("/user/list");
		List<User> users = userService.getUsers();
		users.add(new User());
		mv.addObject("users", users);
		return mv;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView add(@ModelAttribute("form") User user) {
		userService.addUser(user);
		ModelAndView mv = new ModelAndView("/user/list");
		mv.setViewName("redirect:/user/list");
		return mv;
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ModelAndView add(Integer id) {
		userService.remove(id);
		ModelAndView mv = new ModelAndView("/user/list");
		mv.setViewName("redirect:/user/list");
		return mv;
	}
}
